<?php
declare (strict_types=1);

namespace app\admin\validate;

use think\Validate;

class FeedbackForm extends Validate
{
    protected $rule = [
        'name'      => 'require',
        'form_code' => 'require|checkExistCode',
        'status'    => ['require', 'in' => '1,2'],
    ];

    protected $message = [
        'name.require'      => '请输入表单名称',
        'form_code.require' => '请输入表单标识',
        'status'            => '请选择正确的状态',
    ];

    protected $scene = [
        'add'  => ['name', 'form_code', 'status'],
        'edit' => ['name', 'status']
    ];

    public function checkExistCode($value, $rule, $data = [])
    {
        $feedbackFormModel = new \app\common\model\FeedbackForm([], $data['website_code']);
        $feedbackForm      = $feedbackFormModel->findFeedbackFormByCode($value);
        if (!empty($feedbackForm)) {
            return '表单标识已存在';
        }

        return true;
    }

}
